using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System.Linq;

public class Score : MonoBehaviour {

    private string playerName = "";
    private enum ScoreMenuState {ShowRunningScore, SubmitScore, Scoreboard }
    private static ScoreMenuState scoreMenuState;
    public static int cost = 0;
    public static int landedPlanes = 0;
    void OnGUI() {
        if(scoreMenuState == ScoreMenuState.ShowRunningScore)
        GUILayout.Label("Running Costs: " + cost + " Planes Landed: " + landedPlanes);
        if (GUILayout.Button("Start Game!")) {
            Constants.gameState = Constants.GameState.Playing;
        }

        if (scoreMenuState == ScoreMenuState.SubmitScore) {
            GUILayout.BeginHorizontal();
            GUILayout.Label("Enter Player Name: ");
            playerName  = GUILayout.TextField(playerName);
            if (GUILayout.Button("Submit Score")) {
                SaveScore(cost + "_" + playerName);
                scoreMenuState = ScoreMenuState.Scoreboard;
            }
            GUILayout.EndHorizontal();
        }

        if (scoreMenuState == ScoreMenuState.Scoreboard) {
            List<string> scores =  new List<string>(); 
            //Get teh scores from prefs and make a list of them
            foreach (string score in PlayerPrefs.GetString("Scores").Split('|')) {
                if (score != "") {
                    scores.Add(score);
                }
            }
            //Add scores to list
            List<string[]> nameScore = new List<string[]>();
            foreach (string scoreName in scores) {
                Debug.Log(scoreName.Split('_')[0]);
                Debug.Log(scoreName.Split('_')[1]);
                nameScore.Add(scoreName.Split('_'));
            }

            //List<string[]> sortedScore = new List<string[]>();
            ////find the highest score
            ////int highestScoreSoFar = int.MinValue;
            //while (nameScore.Count > 0) {
            //    //foreach (string[] unsortesScore in nameScore) {
            //    //    //int testingScore = int.Parse(unsortesScore[0]);
            //    //    //if (testingScore > highestScoreSoFar) {
            //    //        //highestScoreSoFar = testingScore;
            //    //    }
            //    //}
            //    //nameScore.Select
            //    int maxValue = nameScore.Select(a => int.Parse(a[0])).Max();
            //    Debug.Log(maxValue);
            //}

            //Draw the list
            foreach (string[] parsedString in nameScore) {
                GUILayout.Box(parsedString[1] + "\t" + parsedString[0]);
            }
            if(GUILayout.Button("Play Again")){
                Application.LoadLevel(Application.loadedLevel);
                scoreMenuState = ScoreMenuState.ShowRunningScore;
                Interface.currentMenuState = Interface.MenuStates.World;
                Time.timeScale = 1.0f;
            }
        }
    }

    public static void CheckGameDone() {

        Aircraft[] planesActive = FindObjectsOfType(typeof(Aircraft)) as Aircraft[];
        Debug.Log(planesActive.Length);
        if (planesActive.Length == 0) {
            Time.timeScale = 0;
            scoreMenuState = ScoreMenuState.SubmitScore;
            Constants.gameState = Constants.GameState.Planning;
            Interface.activeAircraft = null;
        }
    }

    private static void SaveScore(string score) {
        string currentScores = PlayerPrefs.GetString("Scores");
        PlayerPrefs.SetString("Scores", currentScores + score + "|");
        Debug.Log(PlayerPrefs.GetString("Scores"));
    }


}
